// pages/staff/chooseCourse/applyfree/applyfree.js
const app = getApp()
var CONFIG = require("../../../../config/config.js");
var share = require("../../../../utils/share.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stu: [],
    stu_id: '',
    desc: '',
    textfalse: false,
    yue: '',
    buttonclick: false,
    butfalse:true,
    showfalse: false,
    text: '',
    showfalse1: false,
    text1: '',
    //   moneyfree: [{ index: 0, title: "请选择" }, { index: 1, title: "微信银行卡" }, { index: 2, title: "现金" }, { index: 3, title: "余额" }],   //类型
      moneyfree: [{ index: 0, title: "请选择" }, { index: 3, title: "小七币" }, { index: 2, title: "人民币" }],   //类型
    moneyfree_index: 0,
    bank_index:0,
    bankarr:[],
  },

    // 选择退款方式
    bindareaChange(e){
        var that = this;
        that.setData({
            moneyfree_index: e.detail.value,
        })
    },

    // 选择开户行
    bankChange(e){
      var that = this;
        that.setData({
          bank_index: e.detail.value,
        })
    },

    // 开户行
    banks(){
      var that = this;
      share.wxRequest('crmapi/user/banks',{},function(res){
        // console.log(res);
        if (Number(res.data.errcode) == 0){
            var data = res.data.data;
            var obj = [{ title: "请选择",bank_code:0 }];
            var methods = obj.concat(data);
            that.setData({
              bankarr: methods
            })
        }
      })
    },

    // e.detail.value,

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      let that = this;
      wx.showLoading({
        title: '加载中~',
        mask: true
      })
      // 开户行
      that.banks();
      // console.log(options);
      that.setData({
        stu_id: options.stu_id,
        // stu_id: 56,
      });
      //获取课程顾问信息
        var obj = {
          uc_id:that.data.stu_id
      }
      share.wxRequest('crmapi/user/userCourse',obj,function(res){
          // console.log(res);
          wx.hideLoading();
          that.setData({
            stu: res.data.data
          })
      })
    },
  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  },
  


    sureclick() {
        var that = this;
        that.setData({
            butfalse: true,
            showfalse: false,
        })
    },
    sureclick1() {
        var that = this;
        wx.navigateBack();
    },

    // 提交
    applyfreebtn: function (e) {
        // console.log(e);
        var that = this;
        var e1 = e.detail.value;
        var user_id = that.data.stu_id;
        var stu = that.data.stu;
        var user_id1 = that.data.stu.user_id;
        var tuiprice = e1.return;
        var account_paid = Math.floor((stu.account_paid) * 100) / 100;
        var reg = /(^[-+]?[1-9]\d*(\.\d{1,2})?$)|(^[-+]?[0]{1}(\.\d{1,2})?$)/;
        var re = /^[\u4e00-\u9fa5a-z]+$/gi;//只能输入汉字和英文字母
        var moneyfree_index = that.data.moneyfree_index;
        // else if (Number(tuiprice) == 0) {
        //   wx.showToast({
        //       title: '退款金额不能为0',
        //       icon: 'none'
        //   })
        //   } 
        if (Number(moneyfree_index) == 0){
            wx.showToast({
                title: '请选择退款类型',
                icon: 'none'
            })
        }else if (tuiprice == '') {
            wx.showToast({
                title: '请填写退款金额',
                icon: 'none'
            })
        }else if (!reg.test(tuiprice)) {
            wx.showToast({
                title: '请输入正确的退款金额',
                icon: 'none'
            })
        } else if (Number(tuiprice) > Number(account_paid)) {
            wx.showToast({
                title: '退款金额不能大于已付款',
                icon: 'none'
            })
        }else if (e1.textWhy == '') {
          wx.showToast({
              title: '请填写申请原因',
              icon: 'none'
          })
          // return false;
        } else {
        //   if(Number(moneyfree_index)==1){
        //     if(Number(that.data.bank_index)==0){
        //       wx.showToast({
        //         title: '请选择开户行',
        //         icon: 'none'
        //       })
        //     }else if(e1.banknamw==''){
        //       wx.showToast({
        //         title: '请输入收款人姓名',
        //         icon: 'none'
        //       })
        //     } else if (!(/^[\u2E80-\u9FFF]+$/.test(e1.banknamw))) {
        //       // } else if (!(/^[\u4e00-\u9fa5a-z]+$/gi.test(e1.truename))){
        //         wx.showToast({
        //           title: '收款人姓名只能为汉字',
        //           icon: 'none',
        //           duration: 1500
        //         })
        //     } else if (Number(e1.banknamw.length)>5){
        //       wx.showToast({
        //         title: '收款人姓名最多为5个字',
        //         icon: 'none',
        //         duration: 1500
        //       })
        //     }else if(e1.banknum==''){
        //       wx.showToast({
        //         title: '请输入银行卡卡号',
        //         icon: 'none',
        //         duration: 1500
        //       })
        //     }else{
        //       that.tuishare(e1);
        //     }
        //   }else{
            that.tuishare(e1);
        //   }
          
        }

    },
    tuishare(e1){
      var that = this;
      var stu = that.data.stu;
      wx.showLoading({
        title: '提交中~',
        mask: true
    })
      that.setData({
          butfalse: false,
      })
      var moneyfree_index = that.data.moneyfree_index;
    //   if(Number(moneyfree_index)>=2){
        var banknamw = '';
        var banknum = '';
        var bank_code = '';
        var bank_name = '';
    //   }else{
    //     var banknamw = e1.banknamw;
    //     var banknum = e1.banknum;
    //     var bank_code = that.data.bankarr[that.data.bank_index].bank_code;
    //     var bank_name = that.data.bankarr[that.data.bank_index].title;
    //   }
    var formData = {
      admin_id:app.globalData.admin_id,
      type:1,
      id:that.data.stu_id,
      course_id:stu.course_id,//	是	int	课程ID
      money:e1.return,
      pay_type: that.data.moneyfree[that.data.moneyfree_index].index,
      specs_id:stu.specs_id,//	否	int	规格ID
      discount_id:stu.discount_id,//	否	int	折扣ID
      y_discounts:stu.discounts,//	是	int	课程原价
      reason: e1.textWhy,//	是	string	备注
      bank_user:banknamw,
      bank_num:banknum,
      bank_code:bank_code,
      bank_name:bank_name,
    }
    // console.log(formData);
      share.wxRequest('crmapi/user/submitAudit',formData,function(res){
      // console.log(res);
          wx.hideLoading();
          if (Number(res.data.errcode) == 0){
            wx.setStorageSync('audit_id',res.data.data.audit_id);
              that.setData({
                  showfalse1:true
              })
          }else{
              that.setData({
                  showfalse: true,
                  text: res.data.msg,
              })
          }
      })
    },
})